import 'package:flutter/material.dart';
import 'package:flutter_account_book/utils/alert.dart';
import 'package:flutter_account_book/utils/db/db_helper.dart';
import 'package:get/get.dart';
import 'package:flutter_account_book/pages/home/logic.dart';
import 'package:flutter_account_book/utils/db/entities/account_book.dart';

class HomeDrawer extends StatefulWidget {
  final Function() onSettings;
  final Function(AccountBook accountBook) onSelected;

  const HomeDrawer(
      {super.key, required this.onSettings, required this.onSelected});
  @override
  State<StatefulWidget> createState() => _HomeDrawer();
}

class _HomeDrawer extends State<HomeDrawer> {
  String helloStr = "";
  MyHomeLogic get logic => Get.find<MyHomeLogic>();
  List<AccountBook> books = [];
  @override
  void initState() {
    super.initState();
    var date = DateTime.now();
    setState(() {
      switch (date.hour) {
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
          helloStr = "早上好，来记个账吧~";
          break;
        case 12:
        case 13:
          helloStr = "中午好，来记个账吧~";
          break;
        case 14:
        case 15:
        case 16:
        case 17:
          helloStr = "下午好，来记个账吧~";
          break;
        case 18:
        case 19:
          helloStr = "傍晚好，来记个账吧~";
          break;
        case 20:
        case 21:
        case 22:
        case 23:
          helloStr = "晚上好，来记个账吧~";
          break;
        default:
          helloStr = "通宵对身体不好，早点休息吧~";
          break;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    return Drawer(
      child: Column(
        children: <Widget>[
          DrawerHeader(
            decoration: BoxDecoration(
              color: context.theme.primaryColor,
            ),
            child: Column(
              children: [
                Container(
                  height: 44,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      IconButton(
                          onPressed: () {
                            Navigator.pop(context);
                            widget.onSettings();
                          },
                          icon: const Icon(Icons.settings))
                    ],
                  ),
                ),
                Text(
                  helloStr,
                  style: const TextStyle(color: Colors.white, fontSize: 24),
                )
              ],
            ),
          ),
          Text(
            '我的账本',
            style: TextStyle(color: themeData.indicatorColor),
          ),
          Expanded(
              child: Obx(
            () => ListView.builder(
                padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
                itemCount: logic.state.accountBooks.length + 1,
                itemBuilder: (context, index) {
                  if (index == logic.state.accountBooks.length) {
                    return OutlinedButton.icon(
                        key: const Key("add"),
                        onPressed: () async {
                          String? text = await showTextAlertDialog(
                              context: context, title: "添加账本");
                          print("text: $text");
                          if (text != null) {
                            AccountBook book = AccountBook();
                            book.name = text;
                            book.id = await DbHelper.helper.accountBookDao
                                .getUnusedId();
                            await book.save();
                            logic.reload();
                          }
                        },
                        icon: const Icon(Icons.add),
                        label: const Text("添加账本"));
                  }
                  final accountBook = logic.state.accountBooks[index];
                  return InkWell(
                      key: ValueKey(accountBook.id),
                      onTap: () {
                        // 处理点击事件
                        Navigator.pop(context); // 关闭抽Semantics屉
                        widget.onSelected(accountBook);
                      },
                      child: Container(
                        decoration: index == logic.state.accountBooks.length - 1
                            ? null
                            : const BoxDecoration(
                                border: Border(bottom: BorderSide(width: 0.5))),
                        child: ListTile(
                            title: Text(accountBook.name),
                            contentPadding: EdgeInsets.zero),
                      ));
                }),
          ))
        ],
      ),
    );
  }
}
